package org.jackysoft.struts2.converter;

import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

public class GenericEnumConverter extends StrutsTypeConverter {

	@SuppressWarnings("unchecked")
	@Override
	public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
		if (arg2.isEnum()) {
			if (arg1 == null) {
				return null;
			}
			if (arg1.length == 1) {
				return Enum.valueOf(arg2, arg1[0]);
			} else {
				Object[] oo = new Object[arg1.length];
				for (int i = 0; i < arg1.length; i++) {
					oo[i] = Enum.valueOf(arg2, arg1[i]);
				}
				return oo;
			}

		}
		return null;
	}

	@Override
	public String convertToString(Map arg0, Object arg1) {
		return arg1.toString();
	}

}
